﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Appsters
{
    public partial class DailyView : Form
    {
        private OleDbConnection dbConn;
        
        public DailyView()
        {
            InitializeComponent();

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";

            string connStr = csb.ConnectionString;

            dbConn = new OleDbConnection(connStr);

            content.AutoScroll = true;
            
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void addTask_Click(object sender, EventArgs e)
        {
            addTask.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            DateTime longDate = date.Value;
            string stringDate = date.Value.ToString("yyyy-MM-dd");  
            Tasks b = new Tasks(stringDate, longDate);
            b.Show();
            this.Close();
        }

        private void toPlanner_Click(object sender, EventArgs e)
        {
            toPlanner.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            planner p = new planner();
            p.Show();
            this.Close();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            edit.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            string title = "";
            string subj = "";
            string desc = "";
            string alarm = "";
            string repeat = "";
            string interval = "";

            //get the last tuple in the task database
            dbConn.Open();
            string qryStr = @"SELECT title, subject, description, alarm, repeat, [interval] FROM Task";
            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);
            OleDbDataReader reader = cmd.ExecuteReader();

            try
            {
                while(reader.Read())
                {
                    title = (string)reader.GetString(0);
                    subj = (string)reader.GetString(1);
                    desc = (string)reader.GetString(2);
                    alarm = (string)reader.GetString(3);
                    repeat = (string)reader.GetString(4);
                    interval = (string)reader.GetString(5);
                }
            } catch (Exception n)
            {

            }

            Tasks t = new Tasks(title, subj, desc, alarm, repeat, interval);
            t.Show();
            this.Hide();

            dbConn.Close();
        }

        private void DailyView_Load(object sender, EventArgs e)
        {
            refreshList();
        }

        private void date_ValueChanged(object sender, EventArgs e)
        {
            refreshList();

        }

        private void refreshList()
        {
            content.Controls.Clear();
            
            string theDate = date.Value.ToString("yyyy-MM-dd");

            dbConn.Open();
            
            string qryStr = @"SELECT COUNT(title) FROM Task";
            OleDbCommand cmd = new OleDbCommand(qryStr, dbConn);          
            int countStart = System.Convert.ToInt32(cmd.ExecuteScalar());
           
            string qry = @"SELECT title, subject, description, alarm, repeat, [interval], theDate FROM Task";
            OleDbCommand c = new OleDbCommand(qry, dbConn);
            OleDbDataReader reader = c.ExecuteReader();

            int ix = 0;
            while (reader.Read())
            {             
                string t = (string)reader.GetValue(0);
                string s = (string)reader.GetValue(1);
                string d = (string)reader.GetValue(2);
                string a = (string)reader.GetValue(3);
                string r = (string)reader.GetValue(4);
                string i = (string)reader.GetValue(5);
                string td = (string)reader.GetValue(6);

                if (td == theDate && ix < countStart)
                {
                    Button b = new Button();
                    b.Location = new Point(0, 30 * ix);
                    b.Size = new Size(440, 38);
                    b.BackColor = Color.LightBlue;
                    b.FlatAppearance.BorderSize = 0;
                    b.FlatStyle = FlatStyle.Flat;
                    b.TextAlign = ContentAlignment.MiddleLeft;

                    if(t.Length > 8){
                        t = t.Substring(0, 6) + "...";
                    }

                    if(s.Length > 8){
                        s = s.Substring(0, 6) + "...";
                    }

                    if(d.Length > 8){
                        d = d.Substring(0, 6) + "...";
                    }

                    b.Font = new Font(b.Font.FontFamily, 1.75f * b.Font.Size);
                    b.Text = t +"     ||     " + s + "     ||     " + d;
                    b.Click += (sender, e) =>
                    {
                        bClicked(sender, e, a, r, i);
                    };
                    content.Controls.Add(b);
                    ix++;
                }                
            }
            dbConn.Close();              
        }

        private void bClicked(object sender, EventArgs e, string a, string r, string i)
        {
            alarmBox.Clear();
            string defT = "";
            alarmBox.AppendText(defT + "ALARM DETAILS:\r\n\r\nTime: " + a
                + "\r\nRepeat times: " + r + "\r\nInterval (minutes): " + i);
        }

       

    }
}
